* Re-run tables dropping one state at a time.
* Appendix H

set more off

include "bill-analysis/bill_setup.do"


// regressions {{{
// ----------------------------------------------------------------------------

qui levelsof fips, local(statefips)

matrix lout = (., ., ., ., ., ., ., ., ., ., .)
loc rnames ""

foreach s in 0 `statefips' {  // zero includes row with all states

    di as result "Dropping FIPS = `s'"

    foreach dv in `billdv' `lawdv' laws_tighten laws_loosen {
        matrix B = (`s')

        // main specification, all controls
        qui xtpoisson `dv' L.`shoot' `controls' `rng' & fips != `s', `opt'
        matrix B = (B, _b[L.`shoot'], _se[L.`shoot'])

        // political interactions
        qui xtpoisson `dv' shootX???leg `controls' `rng' & fips != `s', `opt'
        foreach iv of varlist shootX???leg {
            matrix B = (B, _b[`iv'], _se[`iv'])
        }

        // add state-specific trends for bill and law analyses
        if inlist("`dv'", "`billdv'", "`lawdv'") {

            qui xtpoisson `dv' L.`shoot' `controls' i.fips#c.year `rng' & fips != `s', `opt'
            matrix B = (B, _b[L.`shoot'], _se[L.`shoot'])
        }
        else {
            matrix B = (B, ., .)
        }

        matrix lout = lout \ B
        loc rnames "`rnames' `dv'"
    }
} // end loop over states }}}


// clean up matrix of coefficient estimates {{{
// ----------------------------------------------------------------------------
matrix lout = lout[2..., 1...]

loc cnames "fips bshoot seshoot"
foreach iv of varlist shootX???leg {
    loc cnames "`cnames' b`iv' se`iv'"
}
loc cnames "`cnames' btrend setrend"
matrix colnames lout = `cnames'
matrix rownames lout = `rnames'
// }}}


// save coefficients {{{
// ----------------------------------------------------------------------------

clear
qui svmat2 lout, rnames(dv) names(col)

statez fips, from(fips) to(usps)
rename _USPS_ usps
assert !mi(usps)

order usps fips dv bshoot bshootX* btrend seshoot seshootX* setrend

qui reshape long b se, i(fips dv) j(iv) string

la var usps "Dropped state"
la var fips "Dropped FIPS code"
la var dv "Dependent variable"
la var iv "Independent variable"
la var b "Coefficient"
la var se "Standard error"

qui compress
save "`out'/leave_one_out.dta", replace
// }}}

exit
// vim: foldmethod=marker
